﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace DevDemo
{
    public partial class DevFormByLayOut : DevExpress.XtraEditors.XtraForm
    {
        public DevFormByLayOut()
        {
            InitializeComponent();

            this.WindowState = FormWindowState.Maximized;//最大化
        }

        private TableLayoutPanel table = new TableLayoutPanel();

        private void DevFormByLayOut_Load(object sender, EventArgs e)
        {
            #region TableLayOutPanel

            GetTableLayOutData();

            #endregion

            #region demo2

            //List<DrugClass> drugobjs = new List<DrugClass>();

            //for (int i = 0; i < 10; i++)
            //{
            //    drugobjs.Add(new DrugClass
            //    {
            //        DrugID = "id" + i,
            //        DrugName = "Name" + i,
            //        Drug_Manufacturer = "[厂商" + i + "]",
            //        Drug_Specification = "[100*10" + i + "]"
            //    });
            //}

            //int intx = 10;

            //for (int i = 0; i < 4; i++)
            //{
            //    var ob = new UC_DrugLists();

            //    ob.drugobjs = drugobjs;

            //    ob.TypeName = "精神治疗药物";

            //    ob.Location = new Point(intx, 10);

            //    this.xtraTabControl1.TabPages[0].Controls.Add(ob);

            //    intx += 200;
            //}

            #endregion

            #region demo3

            var objCharge = new UC_ChargeItem();

            this.panel1.Controls.Add(objCharge);

            #endregion
        }

        /// <summary>
        /// 初始化TablelayOutPanel
        /// </summary>
        public void GetTableLayOutData()
        {
            table.Dock = DockStyle.Left;                         //填充
            this.xtraTabControl1.TabPages[0].AutoScroll = true;  //滚动条
            this.xtraTabControl1.TabPages[0].Controls.Add(table);//添加控件
            table.RowCount = 1;                                  //1行

            #region DemoData

            List<DrugClass> drugobjs = new List<DrugClass>();

            for (int i = 0; i < 10; i++)
            {
                drugobjs.Add(new DrugClass
                {
                    DrugID = "id" + i,
                    DrugName = "Name" + i,
                    Drug_Manufacturer = "[厂商" + i + "]",
                    Drug_Specification = "[100*10" + i + "]"
                });
            }

            #endregion

            for (int i = 0; i < 4; i++)
            {
                AddColunm(drugobjs, i.ToString());
            }
        }

        /// <summary>
        /// 添加列方法
        /// </summary>
        /// <param name="objecs"></param>
        private void AddColunm(List<DrugClass> objecs, string tagID)
        {
            //动态添加一列
            table.ColumnCount++;
            //设置高度,边框线也算高度，所以将40修改大一点
            table.Width = table.ColumnCount * 230;
            //行高
            table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 230));
            //设置cell样式，增加线条
            table.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetPartial;

            int i = table.ColumnCount - 1;

            UC_DrugLists_ListView ob = new UC_DrugLists_ListView();

            ob.drugobjs = objecs;

            ob.TypeName = "精神治疗药物" + tagID;

            ob.Tag = tagID;

            ob.SetDelMethod += new UC_DrugLists_ListView.SetDeleteControls(ob_SetDelMethod);

            ob.Dock = DockStyle.Fill;

            table.Controls.Add(ob, i, 0);
        }

        /// <summary>
        /// 删除项目类型
        /// </summary>
        /// <param name="TypeCode"></param>
        private void ob_SetDelMethod(string TypeCode)
        {
            MessageBox.Show("Del Type Code :" + TypeCode);

            RemoveControls(this.xtraTabControl1.TabPages[0], table, TypeCode);
        }

        /// <summary>
        /// 删除控件方法 Col
        /// </summary>
        /// <param name="items"></param>
        /// <param name="tagId"></param>
        public void RemoveControls(Control items, TableLayoutPanel table, string tagId)
        {
            foreach (var item in items.Controls)
            {
                if (item.GetType().ToString().ToUpper().Contains("UC_DRUGLISTS"))
                {
                    var b = (UC_DrugLists)item;

                    if (b.Tag.ToString() == tagId)
                    {
                        int ColIndex = table.GetRow(b);

                        table.Controls.RemoveAt(ColIndex);

                        // 移动,当前行row的下行往上移动
                        for (int k = ColIndex; k < table.ColumnCount - 1; k++)
                        {
                            Control ctlNext = table.GetControlFromPosition(k + 1, 0);

                            table.SetCellPosition(ctlNext, new TableLayoutPanelCellPosition(k, 0));
                        }

                        table.ColumnStyles.RemoveAt(table.ColumnCount - 1);

                        table.ColumnCount = table.ColumnCount - 1;

                        break;
                    }
                }
                else
                {
                    RemoveControls((Control)item, table, tagId);
                }
            }

            // 重新计算高度，否则最后一行偏大
            table.Width = table.ColumnCount * 230;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            MessageBox.Show(this.xtraTabControl1.SelectedTabPageIndex.ToString());
        }
    }
}